package com.sg.business.management.handler;

import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Document;
import com.sg.business.model.DocumentDefinition;
import com.sg.business.model.Folder;
import com.sg.business.model.Organization;
import com.sg.business.model.WorkDefinition;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.IRefreshablePart;
import com.sg.widgets.part.editor.DataObjectDialog;
import com.sg.widgets.viewer.ViewerControl;

public class CreateDocumentDefOfWorkDef extends AbstractNavigatorHandler {

	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		try {
			PrimaryObject master = vc.getMaster();
			if (master instanceof WorkDefinition) {
				WorkDefinition workDefinition = (WorkDefinition) master;
				DocumentDefinition definitiond = workDefinition
						.makeDocumentDefinition(null);
				Organization org = workDefinition.getOrganization();
				List<Folder> roots = org.getContainers();
				definitiond.setValue(Document.F_INIT_FOLDER_ID, roots);
				DataObjectDialog dialog = DataObjectDialog
						.openDialog(
								definitiond,
								DocumentDefinition.EDITOR_DOCUMENT_DEFINITION_CREATE_STANDLONE,
								true, null);
				if (dialog.getReturnCode() == DataObjectDialog.OK) {
					((IRefreshablePart) part).doRefresh();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	protected boolean nullSelectionContinue(IWorkbenchPart part,
			ViewerControl vc, Command command) {
		return true;
	}

}
